<?php
namespace app\admin\controller;

use app\common\cache\common\IAreaCache;
use app\common\controller\AdminBaseController;
use app\common\enum\IsLeader;
use app\common\enum\IsDelete;
use app\common\ext\IDb;
use app\common\ext\IRequest;
use think\Db;

class Cooperate extends AdminBaseController{

    /**
     * @功能：合作商列表
     * @开发者：wdd
     * @return string
     */
    public function cooperate_list(){
        //接收参数
        $cooperate_id = IRequest::get('cooperate_id');
        $cooperate_name = IRequest::get('cooperate_name');
        $area = IRequest::get('area');

        //整理区域数据
        $province_id = 0;
        $city_id = 0;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
        }

        //设置查询条件
        $cooperate_where = null;
        if(!empty($cooperate_id)) $cooperate_where['cooperate_id'] = $cooperate_id;
        if(!empty($cooperate_name)) $cooperate_where['cooperate_name'] = array("like","%{$cooperate_name}%");

        //取得管理员详情
        $cooperate_list = IDb::getInstance('cooperate')->setDbWhere($cooperate_where)->setDbOrder("cooperate_id desc")->pag();
        if ($cooperate_list === false){
            error('获取管理员列表失败');
        }

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        //页面赋值
        $this->assign("cooperate_list",$cooperate_list);
        $this->assign("cooperate_id",$cooperate_id);
        $this->assign("cooperate_name",$cooperate_name);
        $this->assign("area",$area);
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();

    }

    /**
     * @功能：添加合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_add(){

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(2);

        $this->assign("area_list",$area_list);

        return view();
    }

    /**
     * @功能：编辑合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_edit(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //取得属性分类列表
        $area_list = IAreaCache::getAllList(3);

        $cooperate_where['cooperate_id'] = $cooperate_id;
        $cooperate_info = IDb::getInstance('cooperate')->setDbWhere($cooperate_where)->row();
        if ($cooperate_info === false){
            error('获取合作商失败');
        }

        //取得代理商信息
        $cooperate_admin_where['cooperate_id'] = $cooperate_id;
        $cooperate_admin_where['is_leader'] = IsLeader::Yes;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->row();
        if ($cooperate_admin_info === false){
            error('获取合作商负责人失败');
        }

        //整理区域数据
        $cooperate_info['area'] = array($cooperate_info['province_id'],$cooperate_info['city_id'],$cooperate_info['area_id']);

        //页面赋值
        $this->assign("cooperate_id",$cooperate_id);
        $this->assign("cooperate_info",$cooperate_info);
        $this->assign("cooperate_admin_info",$cooperate_admin_info);
        $this->assign("area_list",$area_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：编辑合作商提交
     * @开发者：wdd
     * @return string
     */
    public function cooperate_submit(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id");
        $cooperate_name = IRequest::get("cooperate_name",IRequest::NOT_EMPTY,"合作商名称不能为空！");
        $area = IRequest::get("area");
        $address = IRequest::get("address");
        $remark = IRequest::get("remark");
        $cooperate_admin_id = IRequest::get("cooperate_admin_id");
        $nick = IRequest::get("nick",IRequest::NOT_EMPTY,"负责人昵称错误！");
        $phone = IRequest::get("phone",IRequest::MOBILE,"负责人手机号码错误！");
        $real = IRequest::get("real");
        $avatar = IRequest::get("avatar");
        $identity = IRequest::get("identity");

        //判断城市是否正确
        $province_id = 0;
        $city_id = 0;
        $area_id = null;
        if(!empty($area) && is_array($area)){
            if(!empty($area[0])) $province_id = $area[0]; //取得省份
            if(!empty($area[1])) $city_id = $area[1]; //取得城市
            if(!empty($area[2])) $area_id = $area[2]; //取得城市
        }

        //判断是否为空
        if($province_id <= 0 || $city_id <= 0){
            error('请选择所在的省份城市！');
        }

        //判断名称是否已经存在
        $cooperate_where = null;
        $cooperate_where['cooperate_name'] = $cooperate_name;
        $cooperate_info = IDb::getInstance('cooperate')->setDbWhere($cooperate_where)->row();
        if ($cooperate_info === false) {

            //提示错误信息
            error("获取合作商信息失败！");
        }

        //开启事物处理
        IDb::dbStartTrans();

        //判断是否为新添加数据
        if(empty($cooperate_id)) {

            if (!empty($cooperate_info)){
                error('合作商名称已被使用');
            }

            //添加合作商公司
            $cooperate_data = null;
            $cooperate_data['cooperate_name'] = $cooperate_name;
            $cooperate_data['province_id'] = $province_id;
            $cooperate_data['city_id'] = $city_id;
            $cooperate_data['area_id'] = $area_id;
            $cooperate_data['address'] = $address;
            $cooperate_data['remark'] = $remark;
            $cooperate_data['create_time'] = get_date_time();
            $cooperate_data['is_delete'] = IsDelete::No;
            $cooperate_add = IDb::getInstance('cooperate')->setDbData($cooperate_data)->add();
            if ($cooperate_add === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加合作商基础信息失败，请联系管理员！");
            }

            //判断名称是否重复
            $cooperate_admin_where = null;
            $cooperate_admin_where['phone'] = $phone;
            $cooperate_admin_where['cooperate_id'] = $cooperate_add;
            $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->row();
            if (!empty($cooperate_admin_info)) {

                //提示错误信息
                error("管理员手机号已经被注册了，请重新输入！");
            }

            //添加代理商负责人
            $cooperate_admin_data = null;
            $cooperate_admin_data['cooperate_id'] = $cooperate_add;
            $cooperate_admin_data['pwd'] = md5(md5('88888888'));
            $cooperate_admin_data['phone'] = $phone;
            $cooperate_admin_data['nick'] = $nick;
            $cooperate_admin_data['real'] = $real;
            $cooperate_admin_data['avatar'] = $avatar;
            $cooperate_admin_data['identity'] = $identity;
            $cooperate_admin_data['is_leader'] = IsLeader::Yes;
            $cooperate_admin_data['reg_ip'] = get_client_ip();
            $cooperate_admin_data['login_count'] = 0;
            $cooperate_admin_data['reg_time'] = get_date_time();
            $cooperate_admin_add = IDb::getInstance('cooperate_admin')->setDbData($cooperate_admin_data)->add();
            if($cooperate_admin_add === false){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("添加合作商负责人失败！");
            }

        }else{
            //判断名称是否已经存在
            if (!empty($cooperate_info) && $cooperate_info['cooperate_id'] != intval($cooperate_id)){
                error('合作商名称已被使用');
            }

            //编辑信贷公司
            $cooperate_data = null;
            $cooperate_where = null;
            $cooperate_where['cooperate_id'] = $cooperate_id;
            $cooperate_data['cooperate_name'] = $cooperate_name;
            $cooperate_data['province_id'] = $province_id;
            $cooperate_data['city_id'] = $city_id;
            $cooperate_data['area_id'] = $area_id;
            $cooperate_data['address'] = $address;
            $cooperate_data['remark'] = $remark;
            $cooperate_data['create_time'] = get_date_time();
            $cooperate_data['is_delete'] = IsDelete::No;
            $cooperate_upd = IDb::getInstance('cooperate')->setDbData($cooperate_data)->setDbWhere($cooperate_where)->upd();
            if ($cooperate_upd === false) {
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("编辑合作商基础信息失败，请联系管理员！");
            }

            //判断名称是否重复
            $cooperate_admin_where = null;
            $cooperate_admin_where['phone'] = $phone;
            $cooperate_admin_where['cooperate_id'] = $cooperate_id;
            $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->row();
            if (!empty($cooperate_admin_info) && $cooperate_admin_info['cooperate_admin_id'] != intval($cooperate_admin_id)) {
                //提示错误信息
                error("管理员手机号已经被注册了，请重新输入！");
            }

            //添加代理商负责人
            $cooperate_admin_data = null;
            $cooperate_admin_where = null;
            $cooperate_admin_where['cooperate_admin_id'] = $cooperate_admin_id;
            $cooperate_admin_data['phone'] = $phone;
            $cooperate_admin_data['nick'] = $nick;
            $cooperate_admin_data['real'] = $real;
            $cooperate_admin_data['avatar'] = $avatar;
            $cooperate_admin_data['identity'] = $identity;
            $cooperate_admin_upd = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->setDbData($cooperate_admin_data)->upd();
            if($cooperate_admin_upd === false){
                //事物回滚
                IDb::dbRollback();

                //提示错误信息
                error("修改合作商负责人失败！");
            }
        }

        //提交事物处理
        IDb::dbCommit();

        //画面跳转
        $this->success("操作成功！",Url("cooperate_list"));
    }

    /**
     * @功能：禁用合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_del(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //修改管理员信息
        $cooperate_where['cooperate_id'] = $cooperate_id;
        $cooperate_data['is_delete'] = IsDelete::Yes;
        $cooperate_data['update_time'] = get_date_time();
        $cooperate_upd = IDb::getInstance('cooperate')->setDbWhere($cooperate_where)->setDbData($cooperate_data)->upd();
        if($cooperate_upd === false){

            //错误信息
            error("合作商禁用失败！");
        }

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：还原合作商
     * @开发者：wdd
     * @return string
     */
    public function cooperate_restore(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");

        //修改管理员信息
        $cooperate_where['cooperate_id'] = $cooperate_id;
        $cooperate_data['is_delete'] = IsDelete::No;
        $cooperate_data['update_time'] = get_date_time();
        $cooperate_upd = IDb::getInstance('cooperate')->setDbWhere($cooperate_where)->setDbData($cooperate_data)->upd();
        if($cooperate_upd === false){

            //错误信息
            error("合作商禁用失败！");
        }

        //画面跳转
        $this->success("操作成功！");
    }

    /**
     * @功能：业务员列表
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_list(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
        $account = IRequest::get("account");
        $real = IRequest::get("real");
        $nick = IRequest::get("nick");
        $phone = IRequest::get("phone");
        $is_leader = IRequest::get("is_leader");

        $cooperate_admin_where['cooperate_id'] = $cooperate_id;
        if(!empty($account)) $cooperate_admin_where['account'] = array("like","%{$account}%");
        if(!empty($real)) $cooperate_admin_where['real'] = array("like","%{$real}%");
        if(!empty($nick)) $cooperate_admin_where['nick'] = array("like","%{$nick}%");
        if(!empty($phone)) $cooperate_admin_where['phone'] = array("like","%{$phone}%");
        if(!empty($is_leader)) $cooperate_admin_where['is_leader'] = $is_leader;
        $cooperate_admin_list = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_admin_where)->pag();
        if ($cooperate_admin_list === false){
            error('获取业务员列表失败');
        }

        //页面赋值
        $this->assign("cooperate_id",$cooperate_id);
        $this->assign("account",$account);
        $this->assign("real",$real);
        $this->assign("nick",$nick);
        $this->assign("phone",$phone);
        $this->assign("is_leader",$is_leader);
        $this->assign("cooperate_admin_list",$cooperate_admin_list);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：合作商详情
     * @开发者：wdd
     * @return string
     */
    public function cooperate_info(){
        //接收参数
        $cooperate_id = IRequest::get("cooperate_id",IRequest::NOT_EMPTY,"信贷公司编号不能为空！");
        
        $where['cooperate_id'] = $cooperate_id;
        $cooperate_info = IDb::getInstance('cooperate')->setDbWhere($where)->row();
        if ($cooperate_info === false){
            error('获取合作商详情失败');
        }

        //取得代理商信息
        $cooperate_where['cooperate_id'] = $cooperate_id;
        $cooperate_where['is_leader'] = IsLeader::Yes;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($cooperate_where)->row();
        if ($cooperate_admin_info === false){
            error('获取代理商负责人失败');
        }

        //设置layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("cooperate_info",$cooperate_info);
        $this->assign("cooperate_admin_info",$cooperate_admin_info);

        //渲染模板输出
        return view();
    }

    /**
     * @功能：贷经理详情
     * @开发者：wdd
     * @return string
     */
    public function cooperate_admin_info(){
        //接收参数
        $cooperate_admin_id = IRequest::get("cooperate_admin_id",IRequest::NOT_EMPTY,"信贷经理编号不能为空！");

        //判断名称是否已经存在
        $where['cooperate_admin_id'] = $cooperate_admin_id;
        $cooperate_admin_info = IDb::getInstance('cooperate_admin')->setDbWhere($where)->row();
        if(empty($cooperate_admin_info)){
            error("取得信贷经理详情失败！");
        }

        //设置页码layout
        $this->layout("layout_empty");

        //页面赋值
        $this->assign("cooperate_admin_info",$cooperate_admin_info);

        //渲染模板输出
        return view();
    }
}